package com.fyzk.utils;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONObject;

public class MyHttpUrlConnection {

	 public static String get(String url) {  
	        StringBuffer sBuffer = new StringBuffer();  
	          
	        try {  
	            URL u = new URL(url);  
	            InputStream in = null;  
	            HttpURLConnection conn = (HttpURLConnection) u.openConnection();  
	            conn.setDoInput(true);  
	            conn.setRequestMethod("GET");  
	            conn.setRequestProperty("Content-Type","application/json; charset=utf-8");  
	            if(conn.getResponseCode() == 400){  
	                InputStream erris = conn.getErrorStream();    
//	                MyFileOut.writeOutputStrem(erris);  
	                System.out.println("错误"+erris);
	            }else if(conn.getResponseCode() == 200){  
	                byte[] buf = new byte[1024];  
	                in = conn.getInputStream();  
	                for (int n; (n = in.read(buf)) != -1;) {  
	                    sBuffer.append(new String(buf, 0, n, "UTF-8"));  
	                }  
	            }  
	            in.close();  
	            conn.disconnect();  
	        }catch (FileNotFoundException e1) {  
	            e1.printStackTrace();  
	        }catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	          
	        return sBuffer.toString();  
	    }  
	      
	      
	    public static JSONObject post(String url, Map<String, String> params) {  
	        StringBuffer sBuffer = map2StringBuffer(params); 
	        System.out.println(""+sBuffer.toString());
	        JSONObject object = postJson(url,sBuffer.toString());  
	        return object;  
	    }  
	     
	    
	    private static JSONObject postJson(String url, String params) {  
	        JSONObject object = null;  
	        try {  
	            URL u = new URL(url);  
	            InputStream in = null;  
	            byte[] buffer = params.getBytes();  
	            HttpURLConnection conn = (HttpURLConnection) u.openConnection();  
	            conn.setDoOutput(true);  
	            conn.setDoInput(true);  
	            conn.setRequestMethod("POST");  
	            conn.setRequestProperty("Content-Length", buffer.length + "");  
	            conn.setRequestProperty("Content-Type","application/json;charset=utf-8");  
	            
	            //错误  ——》异常
	            String	out = conn.getOutputStream().toString(); 
	            
	            System.out.println("getgetgetget----------------------------"+out);
	            /*out.write(buffer);  
	            out.close();  */
	            if(conn.getResponseCode() == 400){  
	                InputStream erris = conn.getErrorStream();    
//	                MyFileOut.writeOutputStrem(erris);  
	                System.out.println("错误"+erris);
	            }else if(conn.getResponseCode() == 200){  
	            	System.out.println("ok==============================");
	                in = conn.getInputStream();  
	                StringBuffer sBuffer = new StringBuffer();  
	                byte[] buf = new byte[1024];  
	                for (int n; (n = in.read(buf)) != -1;) {  
	                    sBuffer.append(new String(buf, 0, n, "utf-8"));  
	                }  
	                object = new JSONObject(sBuffer.toString());  
	                in.close();  
	            }
	            conn.disconnect();  
	        } catch (Exception e) {  
	        	System.out.println("eeeeeeeeeeeeeee----------------------------");
	            e.printStackTrace();  
	        }  
	        return object;  
	    }  
	      
	      
	  
	    private static StringBuffer map2StringBuffer(Map<String, String> params) {  
	        StringBuffer buf = new StringBuffer("");  
	        if (params != null && params.size() > 0) {  
	            Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();  
	            while (it.hasNext()) {  
	                Map.Entry<String, String> entry = it.next();  
	                buf.append(entry.getKey()).append("=").append(entry.getValue()).toString();  
	                buf.append("&");  
	            }  
	        }  
	        if (buf.length() > 1)  
	            buf.deleteCharAt(buf.length() - 1);  
	        return buf;  
	    }  
	    
	    public static void MyHttpPost(){
	    	
	    }
}
